



export default {

  //保存cookie之类
  saveTokenId: function (tokenId, isRemember) {
    if (isRemember) {
      localStorage.setItem("tokenId", tokenId);
    } else {
      localStorage.removeItem("tokenId")
      sessionStorage.setItem("tokenId", tokenId)
    }
  },


  load: {
    requestNum: 0,
    countRequest: function () {
      if (this.requestNum < 0) {
        this.requestNum = 0;
      }
      this.requestNum++;
      return this.loading()
    },

    countResponse: function () {
      this.requestNum--;
      if (this.requestNum < 0) {
        this.requestNum = 0;
      }

      return this.loading()
    },

    loading: function () {
      if (this.requestNum === 0) {
        return false
      }
      return true
    },
  },


  //----

  tableData: {
    currentPage: 0,
    totalPage: 0,
    header: [],
    columnList: [],

    // "label": "id",
    //   "prop": "id"

    indexItem: {
      type: 'index', isShow: true,
      width:'65',label:'NO.',
    },
    selectionItem: {
      type: 'selection', isShow: true
    },


    makeProgramHeader: function (list) {
      var programHeader = [this.indexItem]
      list.forEach(function (item) {
        if (item.prop === 'programName') {
          item.isShow = true
          item.type = 'link'
        }

        programHeader.push(item)
      }, this)
      return programHeader
    },

    // "id": 1,
    // "lineName": "line1",
    // "path": "c:/smt/line/line1/program/program1.txt",
    // "programName": "program_name_1"
    makeProgramList: function (list) {
      var programList = []
      list.forEach(function (item) {
        item.programName_link = "./#/scanlist?chanxian=" + item.lineName + "&program=" + item.programName; //这里要programName可点击，所以需要添加属性programName_link
        programList.push(item)
      })
      return programList
    },


    makeProgramFileHeader: function (list) {
      var programHeader = [this.indexItem]
      list.forEach(function (item) {
        if (item.prop === 'programName') {
          item.isShow = true
          item.width = 250
        } else if (item.prop === 'path') {
          item.isShow = true
          // item.type='link'
        }
        programHeader.push(item)
      })
      return programHeader
    },


    makeProgramFileList: function (list) {
      var programList = []
      list.forEach(function (item) {
        // item.path_link = "file://localhost/" + item.path ; //这里要programName可点击，所以需要添加属性programName_link

        item.isShowHighLight=false
        programList.push(item)
      })
      return programList
    },


    makeScanHeader: function (list) {

      var programHeader = [this.selectionItem, this.indexItem]
      list.forEach(function (item) {
        item.isShow = true
        programHeader.push(item)
      }, this)
      return programHeader
    },


    makeScanList: function (list) {

      var programList = []
      list.forEach(function (item) {
        item.scanTime = this.getTimeToDate(item.scanTime)
        item.verifyTime = this.getTimeToDate(item.verifyTime)

        if (item.verifyResult) {
          item.verifyResult = '是'
        } else {
          item.verifyResult = '否'
        }


        if (item.verifyResult) {
          item.verifyResult = '是'
        } else {
          item.verifyResult = '否'
        }

        if (item.isVerify) {
          item.isVerify = '是'
        } else {
          item.isVerify = '否'
        }


        programList.push(item)
      }, this)
      return programList

    },


    makeSpeclibHeader: function (list) {
      var programHeader = [this.indexItem]
      list.forEach(function (item) {
        item.isShow = true
        programHeader.push(item)
      })
      return programHeader
    },


    makeSpeclibList: function (list) {
      var programList = []
      list.forEach(function (item) {
        item.created = this.getTimeToDate(item.created)
        item.updated = this.getTimeToDate(item.updated)
        programList.push(item)
      }, this)
      return programList
    },


    makePage: function (pageNum, pageSize, total) {

      //请求数据pageNum =1，返回pageNum=0，触发了页面切换，请求pageNum=0的数据
      var page = {}
      if (total !== 0) {
        page.currentPage = pageNum
      }else{
        page.currentPage = 1
      }
      page.pageSize = pageSize
      page.total = total

      return page
    },


    makeClientHeader: function (list) {
      var programHeader = [this.indexItem]
      programHeader.push({prop: 'clientname', label: '客户名称', type: 'link', isShow: true})
      return programHeader
    },

    makeClientList: function (list) {
      var programList = []
      list.forEach(function (item) {
        item.clientname_link = "./#/speclib?clientid=" + item.id + "&clientname=" + item.clientname;
        programList.push(item)
      })
      return programList
    },

    //1515566666 to 2017-10-11 10:10:10
    getTimeToDate: function (time) {

      if (time != null) {
        var date = new Date(time)

        // date = date.getFullYear() + "-"
        //   + (date.getMonth() + 1) + "-"
        //   + date.getDate() + " "
        //   + date.getHours() + ":"
        //   + date.getMinutes() + ":"
        //   + date.getSeconds()

        return date.toLocaleDateString() + ' ' + date.toTimeString().split(' ')[0]
      }

      return ''

    },

    setTableHeaderAutoWidth: function (tableHeader, tableData) {

      for (var i = 0; i < tableHeader.length; i++) {
        //prop label isShow width

        if (!tableHeader[i].isShow || !tableHeader[i].prop || !tableHeader[i].label) {
          //没有label的不要管了
          continue
        }

        var prop = tableHeader[i].prop
        var maxWidth = this.getWidth(tableHeader[i].label)+ 18//头部的字体比较大
        // console.log(prop + ":" + maxWidth)
        for (var j = 0; j < tableData.length; j++) {
          // console.log(tableData[j][prop])
          var item = tableData[j][prop].toString()
          var width = this.getWidth(item)
          if (width > maxWidth) {
            maxWidth = width
          }

          tableHeader[i].width = maxWidth
        }
        //最后一个的长度设置为''
      }
      return tableHeader
    },

    getWidth: function (str) {
      var hanzLength = 0;
      var hanzRe = /[\u4E00-\u9FA5]/g;
      if (hanzRe.test(str)) {
        hanzLength = str.match(hanzRe).length
      }
      var otherLength = str.length - hanzLength
      var width = 18 + otherLength * 8.9 + hanzLength * 16 + 18

      if (width>300){
        return 300
      }
      return width
    }
  },

  //-----


  downLoadFile: function (path) {

    try {
      var elemIF = document.createElement("iframe");
      elemIF.src = path;
      elemIF.style.display = "none";
      document.body.appendChild(elemIF);
    } catch (e) {
    }
  },

  copyArr: function (arr) {
    var res = []
    for (var i = 0; i < arr.length; i++) {
      res.push(arr[i])
    }
    return res
  },

  timer:null,
  mySetInterval:function (context,fun) {

    if(this.timer===null){
    }else{
      clearInterval(this.timer);
    }

    this.timer = setInterval(function () {
      fun(context)
    },5000)

  },


  timeoutDo:function (context,fun) {
    var after = 5000
    setTimeout(function () {
      fun(context)
    },after)
  },

  programAutoCount :0,//这个东西是为了防止创建多个programContent导致多个自动请求同时出现设置的

  // getprogramAutoCount:function () {
  //   return this.programAutoCount
  // }

  isCheckedActionNow:function () {
    //备忘录2017sb:扫描记录那块的自动刷新，如果数据勾选中就不要刷新了，没有勾选数据就自动刷新
    var isChecked = sessionStorage.getItem("isCheckedActionNow")
    if(isChecked ===null){
      return false
    }
    return isChecked
  },

  checkedActionNow:function (isChecked) {
    if(isChecked){
      sessionStorage.setItem("isCheckedActionNow", true)
    }else{
      sessionStorage.removeItem("isCheckedActionNow")
    }
  }
}
